package com.troila.cloud.simplify.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfiguration {

	@Autowired
	private RedisConnectionFactory factory;

	@Bean
	@Autowired
	public RedisTemplate<String, Object> getRedisTemplate() {
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer(Thread.currentThread().getContextClassLoader()));
		redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer(Thread.currentThread().getContextClassLoader()));
		redisTemplate.setConnectionFactory(factory);
		return redisTemplate;
	}
	
	@Bean
	@Autowired
	public RedisTemplate<String, String> getStringRedisTemplate() {
		RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(null);
		redisTemplate.setValueSerializer(null);
		redisTemplate.setConnectionFactory(factory);
		return redisTemplate;
	}

}
